import 'dart:io';
import 'package:path_provider/path_provider.dart';

class DownImage {
  /// 获取应用总缓存大小 (单位: MB)
  static Future<double> getTotalCacheSize() async {
    double totalSize = 0;
    final tempDir = await getTemporaryDirectory();
    totalSize += await _getFolderSize(tempDir);
    return totalSize;
  }

 

  /// 计算文件夹大小
  static Future<double> _getFolderSize(Directory dir) async {
    if (!dir.existsSync()) return 0;

    int totalBytes = 0;
    final files = dir.listSync(recursive: true);

    await Future.forEach(files, (file) async {
      if (file is File) {
        totalBytes += await file.length();
      }
    });

    return totalBytes / (1024 * 1024);
  }
}
